package cn.icare.cms.impl;

import cn.icare.cms.CommonCodeUtil;
import cn.icare.cms.ListUtil;
import cn.icare.cms.MoneyInService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.MoneyInMapper;
import cn.icare.cms.dao.po.Area;
import cn.icare.cms.dao.po.Member;
import cn.icare.cms.dao.po.MoneyIn;
import cn.icare.cms.model.ro.AddMoneyInRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MoneyInServiceImpl implements MoneyInService {
    @Resource
    private MoneyInMapper moneyInMapper;

    private final Logger logger = LoggerFactory.getLogger(MoneyInServiceImpl.class);

    @Override
    public Long createOne(AddMoneyInRO ro) {
        try {
            MoneyIn moneyIn = new MoneyIn();
            moneyIn.setCode(CommonCodeUtil.getCodeByUUId());
            moneyIn.setCreatetime(new Date());
            moneyIn.setOrderid(ro.getOrderid());
            moneyIn.setOrdercode(ro.getOrdercode());
            moneyIn.setMembername(ro.getMembername());
            moneyIn.setAmount(ro.getAmount());
            moneyIn.setPaytype(ro.getPaytype());
            moneyIn.setPayaccount(ro.getPayaccount());
            moneyIn.setInaccount(ro.getInaccount());
            moneyIn.setStatus(ro.getStatus());
            int result = moneyInMapper.insertSelective(moneyIn);
            if (result > 0) {
                return moneyIn.getId();
            }
        }
        catch (Exception ex)
        {
            this.logger.error("createOne 异常",ex.getMessage());
        }

        return 0L;
    }

    @Override
    public boolean deleteOne(Long id) {
        return this.moneyInMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(MoneyIn.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.moneyInMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);

        }
        return result;
    }

    @Override
    public MoneyIn getOne(Long id) {
        return this.moneyInMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(String code, String ordercode,String memberName
            ,Integer payType,String beginTime,String endTime, Integer pageNum, Integer pageSize
        ,Integer sortType
    ) {

        MsgDatasVO result = new MsgDatasVO();
        try {

            PageHelper.startPage(pageNum, pageSize);
            Example example = new Example(MoneyIn.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(code)) {
                //流水号
                criteria.andEqualTo("code", code);
            }
            if (StringUtil.isNotEmpty(ordercode)) {
                //订单号
                criteria.andEqualTo("ordercode", ordercode);
            }
            if (StringUtil.isNotEmpty(memberName)) {
                //客户名称
                criteria.andLike("membername", "%"+memberName+"%");
            }
            if (payType>0) {
                //支付方式
                criteria.andEqualTo("paytype", payType);
            }
            if(StringUtil.isNotEmpty(beginTime))
            {
                criteria.andGreaterThanOrEqualTo("createtime",beginTime);
            }
            if(StringUtil.isNotEmpty(endTime))
            {
                criteria.andLessThanOrEqualTo("createtime",endTime);
            }
            switch (sortType)
            {
                case 1: //时间
                    example.setOrderByClause("createtime desc");
                    break;
                case 2: //状态
                    example.setOrderByClause("status desc");
                    break;
                case 3: //金额
                    example.setOrderByClause("amount desc");
                    break;
            }
            List<MoneyIn> dataList = this.moneyInMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
